﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Vinamilk.BLL;

public partial class Admin_usercontrols_ucVinamilkDetail : HocLapTrinhWeb.UI.UCBase
{

    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (VinamilkID != -1)
            LoadDataEdit(VinamilkID); 
    }

    protected void ObjectDataSource1ObjectCreating(object sender, ObjectDataSourceEventArgs e)
    {
        var vnnProvinceBll = new v_ProvinceBLL(CurrentPage.getCurrentConnection());
        e.ObjectInstance = vnnProvinceBll;
    }

    protected void DropProvinceDataBound(object sender, EventArgs e)
    {
        dropProvince.Items.Insert(0, new ListItem("Chọn khu vực", "-1"));
    }

    public void LoadDataEdit(int VinamilkID)
    {
        var VinamilkBll = new v_VinamilkBLL(CurrentPage.getCurrentConnection());
        try
        {
            var rVinamilk = VinamilkBll.GetVinamilkByID(VinamilkID);
            if (rVinamilk == null) return;
            dropProvince.SelectedValue = rVinamilk.ProvinceID.ToString();
            dropVinamilkType.SelectedValue = rVinamilk.VinamilkTypeID.ToString();
            hdProvinceID.Value = rVinamilk.ProvinceID.ToString();
            hdVinamilkID.Value = rVinamilk.VinamilkID.ToString();
            hdLa.Value = rVinamilk.Latitude.ToString();
            hdLo.Value = rVinamilk.Longtitude.ToString();
            txtTotalMoney.Text = rVinamilk.TotalMoney.ToString();
            txtLa.Text = rVinamilk.Latitude.ToString().Replace(",", ".");
            txtLo.Text = rVinamilk.Longtitude.ToString().Replace(",", ".");
            txtTitle.Text = rVinamilk.VinamilkName;
            FCKContent.Text = rVinamilk.Description;
            txtAddress.Text = rVinamilk.Address;
            cbxIsActive.Checked = rVinamilk.IsActive;
        }
        catch (Exception)
        {
            SaveValidate.IsValid = false;
            SaveValidate.ErrorMessage = msg.GetMessage("ERR-000013");
        }
    }

    protected void BtnSaveClick(object sender, EventArgs e)
    {
        if (SaveData())
            Response.Redirect("~/admin/View.aspx?action=Vinamilks");
    }
    protected void BtnSaveAndNewClick(object sender, EventArgs e)
    {
        if (SaveData())
            Response.Redirect("~/admin/View.aspx?action=Vinamilkdetail");
    }
    protected void BtnCancelAddClick(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// Add & Edit ListPrice
    /// </summary>
    /// <returns></returns>
    private bool SaveData()
    {
        try
        {
            var alexaBll = new v_VinamilkBLL(CurrentPage.getCurrentConnection());
            var dt = new dsVinamilk.tbl_VinamilkDataTable();
            var row = dt.Newtbl_VinamilkRow();
            row.VinamilkName = txtTitle.Text;
            row.Address = txtAddress.Text;
            row.VinamilkTypeID = int.Parse(dropVinamilkType.SelectedValue);
            row.ProvinceID = int.Parse(dropProvince.SelectedValue);
            row.Description = FCKContent.Text;
            row.UserID = int.Parse(Session["userid"].ToString());
            row.TotalMoney = int.Parse(txtTotalMoney.Value.ToString());
            row.CreatedDate = DateTime.Now;
            row.UpdatedDate = DateTime.Now;
            row.IsActive = cbxIsActive.Checked;
            row.Latitude = Double.Parse(hdLa.Value.Replace(".", ","));
            row.Longtitude = Double.Parse(hdLo.Value.Replace(".", ","));
            if (VinamilkID == -1)
            {
                dt.Addtbl_VinamilkRow(row);
                if (alexaBll.Add(dt))
                    return true;
            }
            else
            {
                row.VinamilkID = VinamilkID;
                dt.Addtbl_VinamilkRow(row);
                if (alexaBll.Update(dt))
                    return true;
            }
            SaveValidate.IsValid = false;
            SaveValidate.ErrorMessage = msg.GetMessage(alexaBll.getMsgCode());
            return false;
        }
        catch
        {
            SaveValidate.IsValid = false;
            SaveValidate.ErrorMessage = msg.GetMessage("ERR-000003");
            return false;
        }
    }

    public int VinamilkID
    {
        get
        {
            if (!string.IsNullOrEmpty(Request.QueryString["Vinamilkid"]))
                try
                {
                    return int.Parse(Request.QueryString["Vinamilkid"]);
                }
                catch
                {
                    return -1;
                }
            else
                return -1;
        }
    }
    protected void dropVinamilkType_DataBound(object sender, EventArgs e)
    {
        dropVinamilkType.Items.Insert(0, new ListItem("Chọn cửa hàng", "-1"));
    }
    protected void ObjectDataSource2_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
    {
        var vnnVinamilkTypeBll = new v_VinamilkTypeBLL(CurrentPage.getCurrentConnection());
        e.ObjectInstance = vnnVinamilkTypeBll;
    }
}